package com.pay.paytoken.utils;

import android.app.Activity;
import android.app.Dialog;
import android.text.InputType;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.pay.paytoken.R;
import com.zhy.autolayout.AutoLinearLayout;

import appUtils.PayTokenException;
import appUtils.ToastUtil;
import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Joe on 2017/7/11 0011 17:43.
 * SMSVerifyDialog
 */

public class PayPwdVerifyDialog {
    private final Activity mActivity;
    private commitBtnClick mCommitBtnClick;
    private ViewHolder holder;
    private Dialog dialogWindow;

    public PayPwdVerifyDialog(Activity activity) {
        this.mActivity = activity;
        createDialog();
    }

    private void createDialog() {
        dialogWindow = new Dialog(mActivity, R.style.VerifyDialog);
        LayoutInflater inflater = LayoutInflater.from(mActivity);
        View dialogWindowView = inflater.inflate(R.layout.activity_pay_pwd_verify, null);
        dialogWindow.setContentView(dialogWindowView);
        try {
            WindowManager.LayoutParams attributes = dialogWindow.getWindow().getAttributes();
            DisplayMetrics metrics = mActivity.getResources().getDisplayMetrics();
            attributes.width = metrics.widthPixels;
            attributes.height = metrics.heightPixels;
            dialogWindow.getWindow().setAttributes(attributes);
        } catch (Exception e) {
            e.printStackTrace();
            PayTokenException.getInstance().saveExceptionFile("PayPwdVerifyDialog dialog size" +
                    " reset error:" + e.getMessage());
        }
        holder = new ViewHolder(dialogWindowView);
    }

    public void showSMSVerifyDialog() {
        if (dialogWindow != null && !dialogWindow.isShowing()) {
            dialogWindow.show();
        }
        holder.layoutTitleIvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogViewDismiss();
            }
        });
        holder.ibPayPwdVisible.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.etPayPwdInput.getInputType() != InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
                    holder.etPayPwdInput.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    holder.ibPayPwdVisible.setImageResource(R.drawable.icon_pwd_visible);
                } else {
                    holder.etPayPwdInput.setInputType(InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    holder.ibPayPwdVisible.setImageResource(R.drawable.icon_pwd_invisible);
                }
                holder.etPayPwdInput.setSelection(holder.etPayPwdInput.getText().toString().length());
            }
        });
        holder.btnPayPwdInputCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.etPayPwdInput.getText().toString().length() < 6) {
                    ToastUtil.showShort(" 密码长度不能少于6位!");
                    return;
                }
                if (mCommitBtnClick != null) {
                    mCommitBtnClick.onClick(holder.etPayPwdInput.getText().toString());
                }
            }
        });
    }


    public void setCommitBtnClick(commitBtnClick commitBtnClick) {
        this.mCommitBtnClick = commitBtnClick;
    }

    public void setDialogCancelAble(boolean cancelAble) {
        dialogWindow.setCancelable(cancelAble);
    }

    public void setTitle(String title) {
        if (holder != null) {
            holder.layoutTitleTextName.setText(title);
        }
    }

    public void dialogViewDismiss() {
        if (dialogWindow != null && dialogWindow.isShowing()) {
            dialogWindow.dismiss();
        }
    }

    public interface commitBtnClick {
        void onClick(String pwd);
    }


    static class ViewHolder {
        @Bind(R.id.layout_title_iv_left)
        ImageButton layoutTitleIvLeft;
        @Bind(R.id.layout_title_textName)
        TextView layoutTitleTextName;
        @Bind(R.id.layout_title_ivSecond)
        ImageButton layoutTitleIvSecond;
        @Bind(R.id.ib_pay_pwd_visible)
        ImageButton ibPayPwdVisible;
        @Bind(R.id.layout_title)
        AutoLinearLayout layoutTitle;
        @Bind(R.id.et_pay_pwd_input)
        EditText etPayPwdInput;
        @Bind(R.id.btn_pay_pwd_input_commit)
        Button btnPayPwdInputCommit;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
